package com.tensquare.usr.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.usr.pojo.User;
import com.tensquare.usr.service.UserService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private HttpServletRequest request;

    /*
    * 更新Friend粉丝数和user订阅数 /{userid}/{friendid}/{x}
    * */
    @PutMapping("/{userid}/{friendid}/{x}")
    public void updateByUserAndFriend(@PathVariable String userid,@PathVariable String friendid,@PathVariable int x){
        userService.updateByUserAndFriend(userid,friendid,x);
    }



    /*
    * POST
    /user/sendsms/{mobile}
    发送手机验证码*/
    @PostMapping("/sendsms/{mobile}")
    public Result sendsms(@PathVariable String mobile){
        userService.sendsms(mobile);
        return new Result(true, StatusCode.OK,"ok");
    }
    /*POST
    /user/register/{code}
    注册用户
    * */
    @PostMapping("/register/{code}")
    public Result register(@PathVariable String code,@RequestBody User user){
        String redid_code = (String) redisTemplate.opsForValue().get("code_" + user.getMobile());
       if(redid_code==null||"".equals(redid_code)){
            return new Result(false,StatusCode.ERROR,"请先获取手机验证码");
        }
        if(!redid_code.equals(code)){
            return new Result(false,StatusCode.ERROR,"请输入正确的验证码");
        }
        try {
            userService.add(user);
        }catch (Exception e){
            return new Result(false,StatusCode.ERROR,"手机已注册");
        }
        return new Result(true, StatusCode.OK,"ok");

    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        user=userService.login(user);
        if(user==null){
            return new Result(false,StatusCode.ERROR,"登陆失败,请检查用户名或密码");
        }
        //分发token
        String token=jwtUtil.createJWT(user.getId(),user.getMobile(),"user");
        return new Result(true,StatusCode.OK,"登陆成功",token);
    }


    /**
     * 查询全部数据
     * @return
     */
    @RequestMapping(method= RequestMethod.GET)
    public Result findAll(){
        return new Result(true,StatusCode.OK,"查询成功",userService.findAll());
    }

    /**
     * 根据ID查询
     * @param id ID
     * @return
     */
    @RequestMapping(value="/{id}",method= RequestMethod.GET)
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",userService.findById(id));
    }


    /**
     * 分页+多条件查询
     * @param searchMap 查询条件封装
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
    public Result findSearch(@RequestBody Map<String,Object> searchMap , @PathVariable int page, @PathVariable int size){
        Page<User> pageList = userService.findSearch(searchMap, page, size);
        return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<User>(pageList.getTotal(), pageList.getRecords()) );
    }

    /**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map<String,Object> searchMap){
        return new Result(true,StatusCode.OK,"查询成功",userService.findSearch(searchMap));
    }

    /**
     * 增加
     * @param user
     */
    @RequestMapping(method=RequestMethod.POST)
    public Result add(@RequestBody User user  ){
        userService.add(user);
        return new Result(true,StatusCode.OK,"增加成功");
    }

    /**
     * 修改
     * @param user
     */
    @RequestMapping(value="/{id}",method= RequestMethod.PUT)
    public Result update(@RequestBody User user, @PathVariable String id ){
        user.setId(id);
        userService.update(user);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除 必须有admin角色才能删除
     * @param id
     */
    @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    public Result delete(@PathVariable String id ){
        userService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
